package com.silence.whereeat.main;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.silence.whereeat.R;
import com.silence.whereeat.model.FoodInfo;
import com.silence.whereeat.context.CommonURL;
import com.silence.whereeat.utils.ImageDownload;

import java.util.List;

/**
 * Created by KIM on 2015/8/16.
 */
public class MyFoodAdapter extends ArrayAdapter<FoodInfo> {

    private int viewId;
    private LayoutInflater inflater;

    public MyFoodAdapter(Context context, int resourceId, List<FoodInfo> objects) {
        super(context, resourceId, objects);
        viewId = resourceId;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        FoodInfo foodInfo = getItem(position);
        View view = inflater.inflate(viewId, null);
        TextView priceTv = (TextView) view.findViewById(R.id.recommend_item_price);
        TextView introduceTv = (TextView) view.findViewById(R.id.recommend_item_introduce);
        final ImageView recommendImg = (ImageView) view.findViewById(R.id.recommend_item_img);

        priceTv.setText("￥" + foodInfo.getFoodPrice());
        introduceTv.setText(foodInfo.getFoodIntroduce());

        ImageDownload download = new ImageDownload(CommonURL.RECOMMEND_IMG_URL + foodInfo.getFoodImg());
        download.download(new ImageDownload.ImageCallBack() {
            @Override
            public void getBitMap(Bitmap bitmap) {
                if (bitmap != null) {
                    recommendImg.setImageBitmap(bitmap);
                } else {
                    recommendImg.setImageResource(R.drawable.no_pic);
                }
            }
        });
        return view;
    }
}